using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;

using Generic.Algorithms.Sorting;

namespace Generics.Net.Tests
{
    [TestFixture]
    public class SortTester
    {
        IList<int> _list = new List<int>();
        ISortStrategy<int> _strategy;

        public SortTester()
        {
        }

        [TestFixtureSetUp]
        public void Init()
        {
            Console.WriteLine("Testing list with 1000 elements");
            Console.WriteLine("===============================");
        }

        public void CreateList()
        {
            _list = new List<int>();
            Random rand = new Random(1000);
            for (int index = 0; index < 1000; index++)
            {
                _list.Add(rand.Next(1000));
            }
            _strategy.List = _list;
        }

        [Test]
        public void BubbleSort()
        {
            _strategy = new BubbleSortStrategy<int>();
            DoTest();
        }

        [Test]
        public void InsertionSort()
        {
            _strategy = new InsertionSortStrategy<int>();
            DoTest();
        }

        [Test]
        public void SelectSort()
        {
            _strategy = new SelectSort<int>();
            DoTest();
        }

        [Test]
        public void MergeSort()
        {
            _strategy = new MergeSortStrategy<int>();
            DoTest();
        }

        void DoTest()
        {
            //Test Ascending Order
            CreateList();            
            _strategy.Sort(SortDirection.Ascending);
            Assert.AreEqual(IsListSorted(SortDirection.Ascending), true);
            Console.WriteLine("Ascending Sort Comparisions for '{0}' : {1}", _strategy.GetType().Name, _strategy.ComparisionsCount);

            //Test Descending Order
            CreateList();            
            _strategy.Sort(SortDirection.Descending);
            Assert.AreEqual(IsListSorted(SortDirection.Descending), true);
            Console.WriteLine("Descending Sort Comparisions for '{0}' : {1}", _strategy.GetType().Name, _strategy.ComparisionsCount);
        }

        bool IsListSorted(SortDirection direction)
        {
            if (direction == SortDirection.Ascending)
            {
                int prevValue = -999;
                for (int i = 0; i < _list.Count; i++)
                {
                    if (prevValue > _list[i])
                        return false;
                    prevValue = _list[i];
                }
                return true;
            }
            else
            {
                int prevValue = 999;
                for (int i = 0; i < _list.Count; i++)
                {
                    if (prevValue < _list[i])
                        return false;
                    prevValue = _list[i];
                }
                return true;
            }
        }


    }
}
